<template>
  <nav class="nav-menu">
    <div
      v-for="item in menuItems"
      :key="item.key"
      class="menu-item"
      @mouseenter="openDropdown(item.key)"
      @mouseleave="closeDropdown(item.key)"
    >
      <div class="menu-title">{{ item.label }}</div>
      <div
        class="dropdown-menu"
        :class="{ 'active': activeDropdown === item.key }"
      >
        <div
          v-for="child in item.children"
          :key="child.key"
          class="dropdown-item"
          @click="navigateTo(child)"
        >
          {{ child.label }}
        </div>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { menuConfig } from '../data/menuConfig'

const router = useRouter()
const menuItems = ref(menuConfig)
const activeDropdown = ref(null)

function openDropdown(key) {
  activeDropdown.value = key
}

function closeDropdown(key) {
  if (activeDropdown.value === key) {
    activeDropdown.value = null
  }
}

function navigateTo(path) {
  router.push(path)
  activeDropdown.value = null
}
</script>

<style scoped>
.nav-menu {
  display: flex;
  align-items: center;
  gap: 30px;
}

.menu-item {
  position: relative;
  padding: 8px 15px;
  border-radius: 50px;
  cursor: pointer;
  color: #2c3e50;
  transition: all 0.3s ease;
}

.menu-item:hover {
  background: rgba(74, 107, 175, 0.1);
  color: #4a6baf;
}

.menu-title {
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 200px;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 0, 0, 0.05);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 8px 0;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition: all 0.3s ease;
}

.dropdown-menu.active {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.dropdown-item {
  padding: 12px 20px;
  font-size: 15px;
  color: #2c3e50;
  white-space: nowrap;
  transition: all 0.2s ease;
  cursor: pointer;
  border-radius: 8px;
  margin: 2px 8px;
}

.dropdown-item:hover {
  background: rgba(74, 107, 175, 0.1);
  color: #4a6baf;
  transform: translateX(5px);
}

@media (max-width: 768px) {
  .nav-menu {
    flex-wrap: wrap;
    justify-content: center;
    gap: 15px;
    width: 100%;
    margin-top: 15px;
  }

  .menu-item {
    padding: 6px 12px;
  }

  .menu-title {
    font-size: 14px;
  }

  .dropdown-menu {
    min-width: 180px;
  }
}
</style>
